home *** CD-ROM | disk | FTP | other *** search
- /* Sender and Reciever are simple AppleEvent programs that demonstrate */
- /* all the permutations of interaction levels for sending */
- /* and recieving APpleEvents. */
- /* Have fun with them. */
- /* C.K. Haun */
- /* Apple DTS */
- /* this recieves the simple event */
- #include <Types.h>
- #include <memory.h>
- #include <Packages.h>
- #include <Errors.h>
- #include <quickdraw.h>
- #include <fonts.h>
- #include <dialogs.h>
- #include <windows.h>
- #include <menus.h>
- #include <events.h>
- #include <OSEvents.h>
- #include <Desk.h>
- #include <diskinit.h>
- #include <OSUtils.h>
- #include <resources.h>
- #include <toolutils.h>
- #include <AppleEvents.h>
- #include <EPPC.h>
- #include <GestaltEqu.h>
- #include <PPCToolbox.h>
- #include <Processes.h>
- void DoDiskEvents(long dinfo); /* hi word is error code, lo word is drive number */
-
- void DrawMain(WindowPtr drawIt);
-
- Boolean DoSelected(long val);
-
- void InitAEStuff(void);
-
- void DoHighLevel(EventRecord *AERecord);
- void DoDaCall(MenuHandle themenu, long theit);
- OSErr AEOpenHandler(AppleEvent *messagein, AppleEvent *reply, long refIn);
-
- OSErr AEOpenDocHandler(AppleEvent *messagein, AppleEvent *reply, long refIn);
-
- OSErr AEPrintHandler(AppleEvent *messagein, AppleEvent *reply, long refIn);
-
- OSErr AEQuitHandler(AppleEvent *messagein, AppleEvent *reply, long refIn);
-
- OSErr AESimpleHandler(AppleEvent *messagein, AppleEvent *reply, long refIn);
- pascal Boolean idleProc(EventRecord *eventIn, long *sleep, RgnHandle *mouseRgn);
- /* interaction menu items */
- #define kSelfInteractItem 1
- #define kLocalInteractItem 2
- #define kAllInteractItem 3
- short gInteractArray[4] =
- {
- nil, kAEInteractWithSelf, kAEInteractWithLocal, kAEInteractWithAll
- };
-
-
- #define kMBarID 128
- #define kAppleMenu 128
- #define kFileMenu 129
- #define kEditMenu 130
- #define kToolsMenu 131
- #define kResumeMask 1 /* bit of message field for resume vs. suspend */
- MenuHandle gAppleMenuHandle, gFileMenuHandle, gEditMenuHandle, gToolMenuHandle;
- Handle gMymenu; /* my menu bar handle */
-
- AEAddressDesc targetAddress; /* address of the person to get the data from */
-
-
- #define kSimpleEvent 'SIMP'
- #define kSimpleClass 'Simp'
- Boolean gQuit, gInBackground;
- EventRecord gERecord;
- WindowPtr myWindow;
- short gInteractNow = kSelfInteractItem;
- AEIdleUPP gAEIdleUPP;
-
- #ifdef powerc
- QDGlobals qd;
- #endif
-
- main()
- {
- WindowPtr twindow;
- MaxApplZone();
- InitGraf((Ptr)&qd.thePort);
- InitFonts();
- InitWindows();
- InitMenus();
- TEInit();
- InitDialogs(nil);
- InitCursor();
-
- InitAEStuff();
-
- gMymenu = GetNewMBar(kMBarID);
- SetMenuBar(gMymenu);
- gAppleMenuHandle = GetMenuHandle(kAppleMenu);
- gFileMenuHandle = GetMenuHandle(kFileMenu);
- gEditMenuHandle = GetMenuHandle(kEditMenu);
- gToolMenuHandle = GetMenuHandle(kToolsMenu);
-
- AppendResMenu(gAppleMenuHandle, 'DRVR');
- DrawMenuBar();
- CheckItem(gToolMenuHandle, gInteractNow, true);
- GetNewWindow(128, nil, (WindowPtr)-1);
-
- do {
- WaitNextEvent(everyEvent, &gERecord, 30, nil);
- switch (gERecord.what) {
-
- case nullEvent:
- /* no nul processing in this sample */
- break;
- case updateEvt:
- DrawMain((WindowPtr)gERecord.message); /* draw whatever window needs an update */
- break;
- case mouseDown:
- /* first see where the hit was */
- switch (FindWindow(gERecord.where, &twindow)) {
-
- case inDesk: /* if they hit in desk, then the process manager */
- break; /* will switch us out, we don't need to do anything */
- case inMenuBar:
- DoSelected(MenuSelect(gERecord.where));
- break;
-
- case inSysWindow:
- /* pass to the system */
- SystemClick(&gERecord, twindow);
- break;
- case inContent:
- break;
- case inDrag:
- if (twindow == FrontWindow())
- DragWindow(twindow, gERecord.where, &qd.screenBits.bounds);
- break;
- case inGrow:
- case inGoAway:
- /* don't care */
- break;
-
- }
- case mouseUp:
- /* don't care */
- break;
- /* same action for key or auto key */
- case keyDown:
- case autoKey:
- if (gERecord.modifiers & cmdKey)
- DoSelected(MenuKey(gERecord.message & charCodeMask));
- break;
- case keyUp:
- /* don't care */
- break;
- case diskEvt:
- /* I don't do anything special for disk events, this just passes them */
- /* to a function that checks for an error on the mount */
- DoDiskEvents(gERecord.message);
- break;
- case activateEvt:
- /* Draws on a window activate. Other activate/deactivate stuff is */
- /* handled in either the ChangePlane function (for normal shuffling ) */
- /* or in the suspend/resume handler for layer swaps */
- if (gERecord.modifiers & activeFlag)
- DrawMain((WindowPtr)gERecord.message);
- break;
- case networkEvt:
- /* don't care */
- break;
- case driverEvt:
- /* don't care */
- break;
- case app4Evt:
- switch ((gERecord.message >> 24) & 0x0FF) { /* high byte of message */
- case suspendResumeMessage: /* suspend/resume is also an activate/deactivate */
- gInBackground = (gERecord.message & kResumeMask) == 0;
- break;
- }
- break;
- default:
- break;
- /* This dispatches high level events (AppleEvents, for example) */
- /* to our dispatch routine. This is NEW in the event loop for */
- /* System 7 */
- case kHighLevelEvent:
- DoHighLevel(&gERecord);
- break;
-
- }
- } while (gQuit != true);
-
-
- }
-
- /* DoDaCall opens the requested DA. It's here as a seperate routine if you'd */
- /* like to perform some action or just know when a DA is opened in your */
- /* layer. Can be handy to track memory problems when a DA is opened */
- /* with an Option-open */
- void DoDaCall(MenuHandle themenu, long theit)
- {
- long qq;
- char DAname[255];
- GetMenuItemText(themenu, theit, &DAname);
- qq = OpenDeskAcc(DAname);
- }
-
- /* end DoDaCall */
-
- /* DoDiskEvents just checks the error code from the disk mount, */
- /* and puts up the 'Format' dialog (through DIBadMount) if need be */
- /* You can do much more here if you care about what disks are */
- /* in the drive */
- void DoDiskEvents(long dinfo) /* hi word is error code, lo word is drive number */
- {
- short hival, loval, tommy;
- Point fredpoint = {
- 40, 40
- };
- hival = HiWord(dinfo);
- loval = LoWord(dinfo);
- if (hival != noErr) /* something happened */ {
- tommy = DIBadMount(fredpoint, dinfo);
- }
- }
-
- void DrawMain(WindowPtr drawIt)
- {
- BeginUpdate(drawIt);
- SetPort(drawIt);
- MoveTo(40, 70);
- TextFace(bold);
- TextSize(20);
- DrawString("\pThis Does Nothing");
- EndUpdate(drawIt);
- }
-
- Boolean DoSelected(long val)
- {
- short loval, hival;
- Boolean temp = false;
- loval = LoWord(val);
- hival = HiWord(val);
-
- switch (hival) { /* switch off the menu number selected */
- case kAppleMenu: /* Apple menu */
- if (loval != 1) { /* if this was not About, it's a DA */
- DoDaCall(gAppleMenuHandle, loval);
- } else {
- Alert(128, nil); /* do about box */
- }
- break;
- case kFileMenu: /* File menu */
- gQuit = true; /* only edit item */
- break;
- case kEditMenu:
- /* edit menu junk */
- /* don't care */
- break;
- case kToolsMenu:
- if (loval != gInteractNow) {
- CheckItem(gToolMenuHandle, gInteractNow, false);
- CheckItem(gToolMenuHandle, loval, true);
- AESetInteractionAllowed(gInteractArray[loval]);
- gInteractNow = loval;
- }
- }
- HiliteMenu(0);
- }
-
- void InitAEStuff(void)
- {
- OSErr aevtErr = noErr;
- long aLong = 0;
- Boolean gHasAppleEvents = false;
- /* Check this machine for AppleEvents. If they are not here (ie not 7.0)
- * then we exit */
- gHasAppleEvents = (Gestalt(gestaltAppleEventsAttr, &aLong) == noErr);
- /* The following series of calls installs all our AppleEvent Handlers.
- * These handlers are added to the application event handler list that
- * the AppleEvent manager maintains. So, whenever an AppleEvent happens
- * and we call AEProcessEvent, the AppleEvent manager will check our
- * list of handlers and dispatch to it if there is one.
- */
- if (gHasAppleEvents) {
- aevtErr = AEInstallEventHandler(kCoreEventClass, kAEOpenApplication,
- NewAEEventHandlerProc(AEOpenHandler),0, false);
- if (aevtErr) ExitToShell();
-
- aevtErr = AEInstallEventHandler(kCoreEventClass, kAEOpenDocuments,
- NewAEEventHandlerProc(AEOpenDocHandler),0, false);
- if (aevtErr) ExitToShell();
-
- aevtErr = AEInstallEventHandler(kCoreEventClass, kAEPrintDocuments,
- NewAEEventHandlerProc(AEPrintHandler),0, false);
- if (aevtErr) ExitToShell();
-
- aevtErr = AEInstallEventHandler(kCoreEventClass, kAEQuitApplication,
- NewAEEventHandlerProc(AEQuitHandler),0, false);
- if (aevtErr) ExitToShell();
-
- aevtErr = AEInstallEventHandler(kSimpleClass, kSimpleEvent,
- NewAEEventHandlerProc(AESimpleHandler),0, false);
- if (aevtErr) ExitToShell();
-
- /* create a UPP for the AppleEvent idle proc */
- gAEIdleUPP = NewAEIdleProc(idleProc);
-
- }
- else ExitToShell();
- }
-
- /* end InitAEStuff */
-
- void DoHighLevel(EventRecord *AERecord)
- {
-
- AEProcessAppleEvent(AERecord);
-
- }
-
- /* end DoHighLevel */
-
- /* This is the standard Open Application event. You'll get this as one of the (if not the ) */
- /* first events in your application. So, we open up a blank document */
- OSErr AEOpenHandler(AppleEvent *messagein, AppleEvent *reply, long refIn)
- {
- #pragma unused (messagein,reply,refIn)
- /* we of course don't do anything here, since we're background only */
- return(noErr);
- }
-
- /* end AEOpenHandler */
-
- /* Open Doc, opens our documents. Remember, this can happen at application start AND */
- /* anytime else. If your app is up and running and the user goes to the desktop, hilites one */
- /* of your files, and double-clicks or selects Open from the finder File menu this event */
- /* handler will get called. Which means you don't do any initialization of globals here, or */
- /* anything else except open then doc. */
- /* SO-- Do NOT assume that you are at app start time in this */
- /* routine, or bad things will surely happen to you. */
-
- OSErr AEOpenDocHandler(AppleEvent *messagein, AppleEvent *reply, long refIn)
- {
- #pragma unused (reply, refIn)
- /* we of course don't do anything here */
- return(errAEEventNotHandled); /* we have no docs, so no odoc events should come to us */
-
- }
-
- OSErr AEPrintHandler(AppleEvent *messagein, AppleEvent *reply, long refIn)
- { /* no printing handler in yet, so we'll ignore this */
- /* the operation is functionally identical to the ODOC event, with the additon */
- /* of calling your print routine. */
- #pragma unused (reply,refIn)
- /* we of course don't do anything here */
- return(errAEEventNotHandled); /* we have no docs, so no odoc events should come to us */
- }
-
- /* Standard Quit event handler, to handle a Quit event from the Finder, for example. */
- /* ••••• DO NOT CALL EXITTOSHELL HERE ••••• or you will never have a happy life. */
- OSErr AEQuitHandler(AppleEvent *messagein, AppleEvent *reply, long refIn)
- {
- #pragma unused (messagein,refIn)
-
- /* prepQuit sets the Stop flag for us. It does _NOT_ quit, you */
- /* should NEVER quit from an AppleEvent handler. Calling */
- /* ExitToShell here would blow things up */
- gQuit = true;
- return(noErr);
- }
-
- OSErr AESimpleHandler(AppleEvent *messagein, AppleEvent *reply, long refIn)
- {
- OSErr myErr = AEInteractWithUser(kAEDefaultTimeout, nil, gAEIdleUPP);
- if (myErr != errAENoUserInteraction)
- Alert(444, nil);
- return(noErr);
- }
-
- pascal Boolean idleProc(EventRecord *eventIn, long *sleep, RgnHandle *mouseRgn)
- {
- switch (eventIn->what) {
- case nullEvent:
- /* no nul processing in this sample */
- *sleep = 0;
- mouseRgn = nil;
- break;
- case updateEvt:
- case activateEvt:
- DrawMain((WindowPtr)eventIn->message); /* draw whatever window needs an update */
- break;
- case app4Evt:
- switch ((gERecord.message >> 24) & 0x0FF) { /* high byte of message */
- case suspendResumeMessage: /* suspend/resume is also an activate/deactivate */
- gInBackground = (gERecord.message & kResumeMask) == 0;
- break;
- }
- break;
-
-
- }
- return(false); /* I'll wait forever */
- }
-